View Javadoc
1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java 2 * 3 * Copyright (C) 2003 Andreas Arrgard 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 package com.octagroup.foregej.java.lang; 20 import antlr.collections.AST; 21 import com.octagroup.foregej.antlr.NodeWriter; 22 import com.octagroup.foregej.html.HtmlNodeWriter; 23 import com.octagroup.foregej.java.doc.ast.AST_JAVADOC; 24 import com.octagroup.foregej.java.lang.ast.ops.BiOp; 25 import com.octagroup.foregej.java.lang.tok.Comment; 26 import com.octagroup.foregej.java.lang.tok.TOK_JAVA_COMMENT; 27 import com.octagroup.foregej.java.lang.tok.TOK_WS; 28 /*** 29 */ 30 public class JavaNodeWriter extends NodeWriter 31 { 32 /*** 33 * This is a reference to all the comments after the last written ast 34 */ 35 private JavaToken lastHiddenAfter_=null; 36 /*** 37 * When the tokens hidden after an ast is assigned we look for the 38 * last hidden token 39 */ 40 private JavaToken nextSeparator_=null; 41 /*** 42 * should comments be written? 43 */ 44 protected boolean writeComments_=true; 45 /*** 46 * Adds wrapping spec. for java code tokens. 47 * 48 * @param c 49 * @return 50 */ 51 protected int canWrapOn(char c) 52 { 53 if(c==',') { 54 return WRAP_AFTER; 55 } 56 return WRAP_NOT; 57 //return super.canWrapOn(c); 58 } 59 /*** 60 * Adds wrapping spec. for java code asts. 61 * 62 * @param firstAst 63 * @param secondAst 64 * @return 65 */ 66 protected boolean canWrapBetween(AST firstAst,AST secondAst) 67 { 68 if(firstAst instanceof BiOp&&secondAst instanceof BiOp) { 69 return true; 70 } 71 return false; 72 } 73 /*** 74 * Signals if comments should not be written or not. 75 * 76 * @param b should comments be written 77 */ 78 public void setWriteComments(boolean b) 79 { 80 writeComments_=b; 81 } 82 /*** 83 * Writes the comments that are before the supplied ast. 84 * 85 * @param ast 86 */ 87 private void writeCommentsBefore(JavaAST ast) 88 { 89 if(false==writeComments_) return; 90 JavaToken javaToken=findHead((JavaToken)ast.getHiddenBefore()); 91 if(javaToken!=null) { 92 writeComments(javaToken, null); 93 } 94 } 95 /*** 96 * @param ast 97 */ 98 private void assignCommentsAfter(JavaAST ast) 99 { 100 // 101 // As long as the tokens are not reordered by the 102 // antlr grammar this should work... 103 // 104 lastHiddenAfter_=(JavaToken)ast.getHiddenAfter(); 105 findNextSeparator(lastHiddenAfter_); 106 } 107 /*** 108 * Helper method that finds the next separator. 109 * 110 * @param prospect 111 */ 112 private void findNextSeparator(JavaToken prospect) 113 { 114 while(prospect!=null&&(prospect instanceof Comment|| 115 prospect instanceof TOK_WS)){ 116 prospect=(JavaToken)prospect.getHiddenAfter(); 117 } 118 nextSeparator_=prospect; 119 } 120 /*** 121 * Writes the comments before and after. 122 * 123 * @param ast 124 */ 125 public void write(AST ast) 126 { 127 if(ast!=null) { 128 JavaAST javaAST=(JavaAST)ast; 129 writeCommentsBefore(javaAST); 130 assignCommentsAfter(javaAST); 131 // 132 // write the associated javadoc 133 // 134 if(javaAST.getJavadoc()!=null) { 135 writeComment(javaAST.getJavadoc()); 136 newLine(); 137 } 138 } 139 super.write(ast); 140 } 141 /*** 142 * Helper method that finds the head of a list. 143 * 144 * @param list 145 * @return 146 */ 147 private JavaToken findHead(JavaToken list) 148 { 149 if(list==null) { 150 return null; 151 } 152 while(list.getHiddenBefore()!=null){ 153 list=(JavaToken)list.getHiddenBefore(); 154 } 155 return list; 156 } 157 /*** 158 * Write comments before popping indentation 159 * 160 * @param s 161 * @return 162 */ 163 public String popIndentationAndWrite(String s) 164 { 165 writeCommentsUpTo(s); 166 String lastIndent=popIndentation(); 167 write(s); 168 return lastIndent; 169 } 170 /*** 171 * If we write text that matches the first separator - write all the 172 * comments up to that separator. 173 * 174 * @param s 175 */ 176 public void write(String s) 177 { 178 writeCommentsUpTo(s); 179 super.write(s); 180 } 181 /*** 182 * Helper method that writes all the comments up the supplied token. 183 * 184 * @param s 185 */ 186 private void writeCommentsUpTo(String s) 187 { 188 if(nextSeparator_!=null&&s.equals(nextSeparator_.getText())) { 189 // write the comments up to this separator... 190 writeComments(lastHiddenAfter_, nextSeparator_); 191 // find the next separator... 192 findNextSeparator((JavaToken)nextSeparator_.getHiddenAfter()); 193 } 194 } 195 /*** 196 * Helper method that writes the supplied token if it is a comment. 197 * 198 * @param token 199 */ 200 private void writeComment(JavaToken token) 201 { 202 if(token instanceof Comment&&false==isWritten(token)) { 203 // ok, the token is a comment - check if it is a javadoc token, 204 // or just an ordinary token. 205 if(token instanceof TOK_JAVA_COMMENT) { 206 writeComment((TOK_JAVA_COMMENT)token); 207 } else { 208 writeComment((Comment)token); 209 } 210 if(token.hasNewLineAfter()) { 211 newLine(); 212 } 213 } 214 } 215 /*** 216 * Helper method that writes a comment to this node writer. 217 * 218 * @param comment the comment to write. 219 */ 220 public void writeComment(Comment comment) 221 { 222 // write the ordinary comment without wrapping lines 223 boolean oldWrapSetting=canWrapLines_; 224 canWrapLines_=false; 225 write(comment); 226 canWrapLines_=oldWrapSetting; 227 } 228 /*** 229 * Helper method that writes a java comment to this node writer. 230 * 231 * @param comment the comment to write. 232 */ 233 public void writeComment(TOK_JAVA_COMMENT comment) 234 { 235 AST_JAVADOC javaDoc=comment.getAstJavaDoc(); 236 if(javaDoc==null) { 237 // we were not able to parse the javadoc - 238 // use the unparsed version... 239 write(comment.getText()); 240 } else { 241 // write the javadoc token 242 try{ 243 HtmlNodeWriter hnw=new HtmlNodeWriter(); 244 copyThisStateTo(hnw); 245 hnw.write(javaDoc); 246 }catch (RuntimeException e) { 247 System.out.println("Error writing comment:"+comment.getText()); 248 throw e; 249 } 250 } 251 } 252 /*** 253 * Helper method that writes all the comment in the lastHiddenTokens 254 * list up to the supplied token. 255 * 256 * @param javaToken 257 * @param upTo 258 */ 259 private void writeComments(JavaToken javaToken,JavaToken upTo) 260 { 261 while(javaToken!=null&&javaToken!=upTo){ 262 writeComment(javaToken); 263 javaToken=(JavaToken)javaToken.getHiddenAfter(); 264 } 265 } 266 }

This page was automatically generated by Maven